﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace GameName1
{
    /// <summary>
    /// Class that handles the player state and actions.
    /// </summary>
    class Player
    {
        private float _speed;
        private Texture2D _texture;

        public Vector2 Position;
        public int ScreenWidth;
        public int ScreenHeight;
        public int PlayerMovementRangeX;
        public int PlayerMovementRangeY;

        public float Speed
        {
            get { return _speed; }
            set { _speed = Math.Max(1.0f, value); }
        }

        public int Width
        {
            get {return _texture.Width;}
        }

        public int Height
        {
            get{return _texture.Height;}
        }

        public Player() { }

        public Player(float playerSpeed)
        {
            Speed = playerSpeed;
        }

        public void SetScreenAndPlayerLimits(int screenX, int screenY, int cameraX, int cameraY)
        {
            ScreenWidth = screenX;
            ScreenHeight = screenY;
            PlayerMovementRangeX = cameraX;
            PlayerMovementRangeY = cameraY;
        }

        public void LoadTexture(ContentManager contentManager, string textureName)
        {
            _texture = contentManager.Load<Texture2D>(textureName);
        }

        public void SetStartingPosition(float gridStep)
        {
            Position.X = (float)Math.Floor((float)(ScreenWidth / gridStep) / 2) * gridStep;
            Position.Y = (float)Math.Floor((float)(ScreenHeight / gridStep) / 2) * gridStep;
        }

        public void UpdatePlayer()
        {
            CenterInCamera();
        }
        
        /// <summary>
        /// Draws the player on the screen.
        /// </summary>
        /// <param name="spriteBatch">The object that draws the player sprite on the screen.</param>
        public void DrawPlayer(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_texture, Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
        }

        private void CenterInCamera()
        {
            Position.X = (float)Math.Floor((float)(ScreenWidth / Width) / 2) * Width;
            Position.Y = (float)Math.Floor((float)(ScreenHeight / Height) / 2) * Height;
        }
    }
}